use "C:\Users\patri\Dropbox\My Projects\Spring 2021\Empirical IR\Data\threatHR_2.dta" , clear 

cap drop wi_threat btw_threat

xtset ccode year
by ccode: egen btw_threat = mean(threat)
gen wi_threat = threat - btw_threat


logit m_target25 threat, cluster(ccode) or
	
estimates store m1
	  lroc
	
logit m_target25 wi_threat btw_threat, cluster(ccode)
   	estimates store m2
 lroc
  
   gen lnsipri_milgdp = ln((sipri_milgdp*100)+1) 
  
    
  reg lnsipri_milgdp l.lnsipri_milgd threat, cluster(ccode)
       	estimates store m3

    reg lnsipri_milgdp l.lnsipri_milgd  wi_threat btw_threat, cluster(ccode)
     	estimates store m4

   
		lab var threat "Threat"
				lab var wi_threat "(Within) Threat"
				lab var btw_threat "(Between) Threat"

		lab var lnsipri_milgdp "log Military Spending"

  
estout m1 m2 m3 m4, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats(r2 ll N, fmt(%9.2f %9.0f) labels("R2" "Log-Like"  ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order() drop() ///
mlabels ("Model 1" "Model 2" ) ///
title("") 


drop if oced==1
stop

reg f.wi_theta_mean2 wi_v2stfisccap3 wi_threat    $wc  i.year

gen sample = 1 if e(sample)

**Model 1 (total effect of threat)

global wc wi_v2x_polyarchy wi_lnpop wi_lngdp_pc max_civilwar2 


reg f.wi_theta_mean2  wi_threat  $wc i.year if sample==1, vce(hc3)
 estimates store m1
 
 egen std_wi_threat = std(wi_threat) if sample==1

 
 reg f.wi_theta_mean2  std_wi_threat  $wc i.year if sample==1, vce(hc3)
sum F.wi_theta_mean2 if sample==1
 
**threat decreases the DV (i.e. decreases respect for HR)

**Model 2 (effect of threat on capacity)

reg f.wi_v2stfisccap3 wi_threat  $wc  i.year if sample==1
 estimates store m2
 
reg f.wi_v2stfisccap3 std_wi_threat  $wc  i.year if sample==1
 sum wi_v2stfisccap3  if sample==1
 
 
**threat increase capacity 

 reg f.wi_v2stfisccap3 wi_threat  $wc  i.year , vce(hc3)
 estimates store m2_1
 *re-estimating with robust SE's

**Model 3 (effect of threat and capacity on HR)


reg f.wi_theta_mean2 wi_v2stfisccap3 wi_threat    $wc  i.year
**capacity increases respect for HR, and threat decreases it (beta for threat is the direct effect)

 estimates store m3
 suest m2 m3
 
 **Indirect Effect
nlcom (_b[m2_mean:wi_threat] * _b[m3_mean:wi_v2stfisccap3])

reg f.wi_theta_mean2 wi_v2stfisccap3 wi_threat    $wc  i.year , vce(hc3)
 estimates store m3_1
 
 

lab var wi_threat "External Threat (1919)"
lab var wi_v2stfisccap3 "Capacity"
lab var wi_v2x_polyarchy "Democracy"
lab var wi_lnpop  "log of Population"
lab var wi_lngdp_pc  "log of GDP per cap"
lab var max_civilwar2 "Ongoing Civil War"

estout m1 m2_1 m3_1, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( r2 N, fmt(%9.2f %9.0f) labels("R-Sq" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(wi_threat wi_v2stfisccap3 ) drop(*.year ) ///
mlabels ("Model 1" "Model 2" "Model 3" ) ///
title("") 
